package autowus.gui.appointment;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import nauja.utils.jcalendar.JCalendar;
import nauja.utils.jcalendar.components.DefaultTitlePanel;
import nauja.utils.jcalendar.listeners.CalendarEvent;
import nauja.utils.jcalendar.listeners.DateChangeListener;
import nauja.utils.jcalendar.renderers.DefaultCalendarRenderer;
import nauja.utils.jcalendar.renderers.DefaultCalendarTitleRenderer;

/**
 * Example on how to use
 * <code>DateChangeListener</code>.
 *
 * @author Jeremy Morosi
 * @version 1.0
 */
@SuppressWarnings("serial")
public class ExampleDateChangeListener extends JPanel implements DateChangeListener {

    protected JCalendar calendar;

    public ExampleDateChangeListener() {
        super();
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        
        calendar = JCalendar.getInstance();
        calendar.addDateChangeListener(this);
        DefaultCalendarRenderer renderer = (DefaultCalendarRenderer) calendar.getCalendarRenderer();
        renderer.setTitleRenderer(new DefaultCalendarTitleRenderer(DefaultTitlePanel.MODIFY_MONTH | DefaultTitlePanel.EDITABLE));
        calendar.updateCalendar();
        
        add(calendar);
    }

    public static void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(ExampleDateChangeListener.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }

    @Override
    public void dateChanged(CalendarEvent event) {
        if (event.isDateSelection()) {
            System.out.println("Date selected: " + calendar.format("dd MMMM yyyy (EEEE)"));
        }
    }
    
    public static void main(String[] args) {
        setLookAndFeel();
        JFrame frame = new JFrame();
        frame.setTitle("Current date");
        frame.setContentPane(new ExampleDateChangeListener()); // create the calendar
        frame.pack();
        frame.setMinimumSize(frame.getSize());
        frame.setMaximumSize(frame.getSize());
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}